class CaseController {

  def index = { redirect(action: list, params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [caseInstanceList: Case.list(params), caseInstanceTotal: Case.count()]
  }

  def show = {
    def caseInstance = Case.get(params.id)

    if (!caseInstance) {
      flash.message = "Case not found with id ${params.id}"
      redirect(action: list)
    }
    else { return [caseInstance: caseInstance] }
  }

  def delete = {
    def caseInstance = Case.get(params.id)
    if (caseInstance) {
      try {
        caseInstance.delete()
        flash.message = "Case ${params.id} deleted"
        redirect(action: list)
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "Case ${params.id} could not be deleted"
        redirect(action: show, id: params.id)
      }
    }
    else {
      flash.message = "Case not found with id ${params.id}"
      redirect(action: list)
    }
  }

  def edit = {
    def caseInstance = Case.get(params.id)

    if (!caseInstance) {
      flash.message = "Case not found with id ${params.id}"
      redirect(action: list)
    }
    else {
      return [caseInstance: caseInstance]
    }
  }

  def update = {
    def caseInstance = Case.get(params.id)
    if (caseInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (caseInstance.version > version) {

          caseInstance.errors.rejectValue("version", "case.optimistic.locking.failure", "Another user has updated this Case while you were editing.")
          render(view: 'edit', model: [caseInstance: caseInstance])
          return
        }
      }
      caseInstance.properties = params
      if (!caseInstance.hasErrors() && caseInstance.save()) {
        createCaseCatalog(caseInstance)
        flash.message = "Case ${params.id} updated"
        redirect(action: show, id: caseInstance.id)
      }
      else {
        render(view: 'edit', model: [caseInstance: caseInstance])
      }
    }
    else {
      flash.message = "Case not found with id ${params.id}"
      redirect(action: edit, id: params.id)
    }
  }

  def create = {
    def caseInstance = new Case()
    caseInstance.properties = params
    return ['caseInstance': caseInstance]
  }

  def save = {
    def caseInstance = new Case(params)
    if (!caseInstance.hasErrors() && caseInstance.save()) {
      flash.message = "Case ${caseInstance.id} created"
      redirect(action: show, id: caseInstance.id)
    }
    else {
      render(view: 'create', model: [caseInstance: caseInstance])
    }
  }

  def createCaseCatalog (Case caseInstance) {
    if(!caseInstance.catalogId) {
      def caseRoot = Catalog.findAllByName('case')[0]
      def catalog = Catalog.findAllByNameAndParent(caseInstance.name, caseRoot)
      if(catalog) {
        caseInstance.catalogId = catalog.id
      } else {
        catalog = new Catalog(name:caseInstance.name, description:caseInstance.name, parent:caseRoot)
        catalog.save()
        caseInstance.catalogId = catalog.id
      }
      catalog.save()
    }
  }
}
